package phone.book;

import java.util.ArrayList;

import customlists.ContactsCustomListAdapter;

import model.GroupContacts;

import database.GroupContactsAdapter;

import SMS.SMSMessaging;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;


public class GroupOperations extends ListActivity {
	
	private static final int PICK_CONTACT = 0;
	GroupContactsAdapter gca;
	ArrayList<GroupContacts> contactsList;
	ListView lv;
	AlertDialog.Builder alert;
	private Context ctx;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(getGroupName());
		setContentView(R.layout.contactsmain);
		ctx=this;
		lv=getListView();
		gca=new GroupContactsAdapter(getApplicationContext());
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				final int position=arg2;
				alert=new AlertDialog.Builder(ctx);
				alert.setTitle("Remove contact from group");
		    	alert.setMessage("Confrim delete");
		    	alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						removeContact(position);
					}
				});
		    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
		    	alert.create().show();
				return true;
			}
		});
		
		refreshList();
		gca.close();
		
	}

	private void removeContact(int position) {
		int id=contactsList.get(position).getContactid();
		gca.open();
		gca.deleteContactInGroup(id);
		refreshList();
		gca.close();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.grouplistmenu, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case R.id.addContact: addContact();return true;
			case R.id.groupSMS:sendSMS();return true;
			default:return super.onOptionsItemSelected(item);
		}
	}
	private String getGroupName(){
		return getIntent().getExtras().getString("grpName");
	}
	private int getGroupId(){
		return getIntent().getExtras().getInt("grpId");
	}

	private void sendSMS() {
		alert=new AlertDialog.Builder(this);
		alert.setTitle("SMS");
		if(contactsList.size()!=0){
	    	alert.setMessage("Enter Message");
	    	final EditText message=new EditText(this);
	    	alert.setView(message);
	    	alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					String msg=message.getText().toString();
					SMSMessaging ob=new SMSMessaging(getApplicationContext());
					ArrayList<String> phno=ob.getContactNumbers(contactsList);
					ob.sendMessage(phno,msg);
				}
			});
	    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {	
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
		}
		else
			alert.setMessage("No contacts to send SMS");
	    	alert.create().show();
	}
	
	private void addContact() {
		Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
		startActivityForResult(intent, PICK_CONTACT);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode){
			case PICK_CONTACT:
				if(resultCode==RESULT_OK){
					 Uri contactData = data.getData();
				        Cursor c =  managedQuery(contactData, null, null, null, null);
				        if (c.moveToFirst()) {
				           String name = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
				           addContactToGroup(name);
				           refreshList();
				        }
				}
		}
	}
	private void refreshList() {
		gca.open();
		contactsList=gca.fetchContacts(getGroupId());
		lv.setAdapter(new ContactsCustomListAdapter(getApplicationContext(),contactsList));
		gca.close();
	}
	private void addContactToGroup(String name){
        gca.open();
        gca.addContactToGroup(getGroupId(), name);
        gca.close();
	}

}
